<template>
  <el-dialog
    :title="state.type == 'add' ? '添加秒杀' : '修改秒杀'"
    v-model="state.visible"
    width="400px"
  >
    <el-form :model="state.ruleForm" :rules="state.rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="商品编号" prop="goodsId">
        <el-input type="text" v-model="state.ruleForm.goodsId"></el-input>
      </el-form-item>
      <el-form-item label="秒杀价格" prop="seckillPrice">
        <el-input type="number" v-model="state.ruleForm.seckillPrice"></el-input>
      </el-form-item>
      <el-form-item label="秒杀数量" prop="seckillNum">
        <el-input type="number" v-model="state.ruleForm.seckillNum"></el-input>
      </el-form-item>
      <el-form-item label="是否开启秒杀" prop="seckillStatus">
        <el-switch
            v-model="state.ruleForm.seckillStatus"
            active-color="#13ce66"
            inactive-color="#ff4949">
        </el-switch>
      </el-form-item>
      <el-form-item label="开启时间" prop="seckillBegin">
        <el-date-picker
            v-model="state.ruleForm.seckillBegin"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="选择开启时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="seckillEnd">
        <el-date-picker
            v-model="state.ruleForm.seckillEnd"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排序" prop="seckillRank">
        <el-input type="number" v-model="state.ruleForm.seckillRank"></el-input>

      </el-form-item>


    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import axios from '@/utils/axios'
import { ElMessage } from 'element-plus'

const props = defineProps({
  type: String, // 用于判断是添加还是编辑
  reload: Function // 添加或修改完后，刷新列表页
})

const formRef = ref(null)
const route = useRoute()
const state = reactive({
  visible: false,
  ruleForm: {
    goodsId: '',
    seckillPrice: 0,
    seckillNum: 0,
    seckillStatus: false,
    seckillBegin: "",
    seckillEnd: "",
    seckillRank: 0,
  },
  rules: {
    name: [
      { required: 'true', message: '名称不能为空', trigger: ['change'] }
    ],
    rank: [
      { required: 'true', message: '不能为空', trigger: ['change'] }
    ]
  },
  id: ''
})
// 获取详情
const getDetail = (id) => {
  axios.get(`/seckill/${id}`).then(res => {
    state.ruleForm = {
      goodsId: res.goodsId,
      seckillPrice:  res.seckillPrice,
      seckillNum: res.seckillNum,
      seckillStatus:  res.seckillStatus,
      seckillBegin:  res.seckillBegin,
      seckillEnd:  res.seckillEnd,
      seckillRank:  res.seckillRank,
    }
  })
}
// 开启弹窗
const open = (id) => {
  state.visible = true
  if (id) {
    state.id = id
    // 如果是有 id 传入，证明是修改模式
    getDetail(id)
  } else {
    // 否则为新增模式
    // 新增类目，从路由获取分类 level 级别和父分类 id

    state.ruleForm = {
      goodsId: '',
      seckillPrice: 0,
      seckillNum: 0,
      seckillStatus: false,
      seckillBegin: "",
      seckillEnd:  "",
      seckillRank: 0
    }
  }
}
// 关闭弹窗
const close = () => {
  state.visible = false
}
const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (props.type == 'add') {
        // 添加方法
        axios.post('/seckill/save', {
          goodsId: state.ruleForm.goodsId,
          seckillPrice:  state.ruleForm.seckillPrice,
          seckillNum: state.ruleForm.seckillNum,
          seckillStatus: state.ruleForm.seckillStatus,
          seckillBegin: state.ruleForm.seckillBegin,
          seckillEnd: state.ruleForm.seckillEnd,
          seckillRank: state.ruleForm.seckillRank,

        }).then(() => {
          ElMessage.success('添加成功')
          state.visible = false
          // 接口回调之后，运行重新获取列表方法 reload
          if (props.reload) props.reload()
        })
      } else {
        // 修改方法
        axios.post('/seckill/update', {
          seckillId:  state.id,
          goodsId: state.ruleForm.goodsId,
          seckillPrice:  state.ruleForm.seckillPrice,
          seckillNum: state.ruleForm.seckillNum,
          seckillStatus: state.ruleForm.seckillStatus
        }).then(() => {
          ElMessage.success('修改成功')
          state.visible = false
          // 接口回调之后，运行重新获取列表方法 reload
          if (props.reload) props.reload()
        })
      }
    }
  })
}
defineExpose({ open, close })
</script>